package table;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import table.node.BaseNode;
import table.node.DataNode;
import jxl.write.*;
import jxl.*;
import java.io.*;

public class ExcelTable implements DataTable {
	//----- Base -----
	private String beanId;
	private String description;
	private String dataTableName;
	private Col idCol;
	private List<Col> colList;

	//------Flag---------
	private boolean viewFlag;
	private boolean modifyFlag;
	private boolean deleteFlag;
	private boolean insertFlag;

	//----- Excel Table -----
	private Sheet sheet;
	public ExcelTable(Sheet sheet){
		this.setSheet(sheet);
		this.init();
	}
	private void init(){
		this.setDataTableName(this.getSheet().getName());
		this.setDescription(this.getSheet().getName());
		int colSize = this.getSheet().getColumns();
		int rowSize = this.getSheet().getRows();
		List<Col> cols = this.getColList();
		
		if(cols == null){
			cols = new ArrayList<Col>();
			for(int colIndex = 0 ; colIndex < colSize; colIndex++){
				Col col = new Col();
				if(rowSize > 1){
					col.setName(this.getSheet().getCell(colIndex, 0).getContents());
					col.setDescripes(this.getSheet().getCell(colIndex, 0).getContents());
				}
				cols.add(col);
			}
			this.setColList(cols);
		}
		
		if(this.getIdCol() == null){
			this.setIdCol(new Col());
		}
		this.getIdCol().clearData();
		for(int colIndex = 0 ; colIndex < colSize; colIndex++){
			Col col = this.getColList().get(colIndex);
			col.clearData();
			for(int rowIndex = 1 ; rowIndex < rowSize; rowIndex++){
				String nodeValue = "";
				if(colIndex == 0){
					DataNode node = new BaseNode();
					node.setNodeValue(rowIndex);
					this.getIdCol().addData(node);
				}
				Cell cell = this.getSheet().getCell(colIndex, rowIndex);
				nodeValue = cell.getContents();
				DataNode node = new BaseNode();
				node.setNodeValue(nodeValue);
				col.addData(node);
			}
		}
		this.setData(null);
	}
	
	/************************
	 ***** Seter&Geter ******
	 ************************/

	//----- Base -----
	public void setBeanId(String beanId) {
		this.beanId = beanId;
	}
	public String getBeanId() {
		return beanId;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getDescription() {
		return description;
	}
	public void setDataTableName(String dataTableName) {
		this.dataTableName = dataTableName;
	}
	public String getDataTableName() {
		return this.dataTableName;
	}

	public void setIdCol(Col idCol) {
		this.idCol = idCol;
	}
	public Col getIdCol() {
		return this.idCol;
	}

	public void setColList(List<Col> colList) {
		this.colList = colList;
	}
	public List<Col> getColList() {
		return this.colList;
	}
	
	//------Flag---------
	public void setDeleteFlag(boolean deleteFlag) {
		this.deleteFlag = deleteFlag;
	}
	public boolean isDeleteFlag() {
		return deleteFlag;
	}

	public void setModifyFlag(boolean modifyFlag) {
		this.modifyFlag = modifyFlag;
	}
	public boolean isModifyFlag() {
		return modifyFlag;
	}

	public void setViewFlag(boolean viewFlag) {
		this.viewFlag = viewFlag;
	}
	public boolean isViewFlag() {
		return this.viewFlag;
	}

	public void setInsertFlag(boolean insertFlag) {
		this.insertFlag = insertFlag;
	}
	public boolean isInsertFlag() {
		return insertFlag;
	}
	
	private void setSheet(Sheet sheet) {
		this.sheet = sheet;
	}

	private Sheet getSheet() {
		return sheet;
	}

	/***************
	 ***** APIs ****
	 **************/

	public boolean setData(SetDataParamenters p) {
		int colSize = this.getSheet().getColumns();
		int rowSize = this.getSheet().getRows();
		List<Col> cols = this.getColList();
		
		if(cols != null){
			this.getIdCol().clearData();
			for(int colIndex = 0 ; colIndex < colSize; colIndex++){
				Col col = cols.get(colIndex);
				col.clearData();
				for(int rowIndex = 1 ; rowIndex < rowSize; rowIndex++){
					String nodeValue = "";
					if(colIndex == 0){
						DataNode idNode = new BaseNode();
						idNode.setNodeValue(rowIndex);
						this.getIdCol().addData(idNode);
					}
					DataNode node = new BaseNode();
					try{
						Cell cell = this.getSheet().getCell(colIndex, rowIndex);
						nodeValue = cell.getContents();
						node.setNodeValue(nodeValue);
					}catch(Exception e){
					}
					col.addData(node);
				}
			}
		}
		return false;
	}
	

	public boolean setColsDataByCols(List<String> dstColNames,
			List<String> srcColNames) {
		// TODO Auto-generated method stub
		return false;
	}
	
	public boolean clearData() {
		Iterator<Col> colIt = null; 
		this.getIdCol().clearData();
		for(colIt= this.getColList().iterator(); colIt.hasNext(); ){
			Col col = colIt.next();
			col.clearData();
		}
		return false;
	}
	public List<Object> getData() {
		// TODO Auto-generated method stub
		return null;
	}

	public Col getColByName(String colName) {
		Col col = null;
		for(int i=0; i < this.colList.size(); i++){
			Col tCol = this.colList.get(i);
			if(tCol != null && tCol.getName().equalsIgnoreCase(colName)){
				col = tCol;
				break;
			}
		}
		if(col == null){
			if(this.getIdCol().getName().equalsIgnoreCase(colName)){
				col = this.getIdCol();
			}
		}
		return col;
	}
	
	public int getPageCount(SetDataParamenters p) {
		try{
			return	this.getSheet().getRows();
			
		}catch(Exception e){}
		
		return 0;
	}

	public List<Row> getRowList() {
		// TODO Auto-generated method stub
		return null;
	}
	
	//--------------------------------------------------- 
	//----data Modify(insert,modify,delete) functions----
	//---------------------------------------------------
	public boolean delete(List<Row> rowList) {
		// TODO Auto-generated method stub
		return false;
	}
	public boolean insert(List<Row> rowList) {
		// TODO Auto-generated method stub
		return false;
	}
	public boolean modify(List<Row> rowList) {
		return false;
	}
	public boolean modify(Row row) {
		// TODO Auto-generated method stub
		return false;
	}

	public DataNode run(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}
	public Col getDataIdCol() {
		// TODO Auto-generated method stub
		return null;
	}

}
